package deepbrain.simnetwork.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Counter<Hashable> {

	private Map<Hashable, Integer> map;
	private int totalCount = 0;

	public Counter() {
		map = new HashMap<Hashable, Integer>();
	}

	public void increase(Hashable key) {
		if (!(map.containsKey(key)))
			map.put(key, 0);
		int count = map.get(key);
		map.put(key, count + 1);
		totalCount++;
	}

	public void increase(Hashable key, int numToIncrease) {
		if (!(map.containsKey(key)))
			map.put(key, 0);
		int count = map.get(key);
		map.put(key, count + numToIncrease);
		totalCount += numToIncrease;
	}

	public int getCount(Hashable key) {
		return map.get(key);
	}

	public Set<Hashable> getCountSet() {
		return map.keySet();
	}

	public List<Hashable> getSortedCountList() {
		List<Hashable> list = new ArrayList<Hashable>(map.keySet());
		Collections.sort(list, new Comparator<Hashable>() {
			public int compare(Hashable o1, Hashable o2) {
				if (map.get(o1) < map.get(o2))
					return 1;
				else
					return -1;
			};
		});
		return list;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public double getPorportion(Hashable key) {
		return (double) getCount(key) / (double) totalCount;
	}

}
